'''
Created on May 8, 2012

@author: lavang
'''
import unittest
from Interface.professorPage import xpathProjects
from Interface.projectPage import xpathLinkAddProject, xpathTxtTitle,\
    xpathCbbDeadlineDay, xpathCbbDeadlineMonth, xpathCbbDeadlineYear,\
    xpathCbbDeadlineHour, xpathCbbDeadlineMinute, nameChkAutoAssign,\
    xpathBtnSubmitProject, xpathLinkProjects
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

def clickOnProjects(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathProjects())
    element.click()
    
def clickOnAddProject(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkAddProject())
    element.click()
    
def addProject(self,title,dlDay,dlMonth,dlYear,dlHour,dlMinute,description,assign='on'):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathTxtTitle())
    element.clear()
    element.send_keys(title)
    
    # Select Deadline
    element=browser.find_element_by_xpath(xpathCbbDeadlineDay())
    Select(element).select_by_visible_text(dlDay)
    
    element=browser.find_element_by_xpath(xpathCbbDeadlineMonth())
    Select(element).select_by_visible_text(dlMonth)
    
    element=browser.find_element_by_xpath(xpathCbbDeadlineYear())
    Select(element).select_by_visible_text(dlYear)
    
    element=browser.find_element_by_xpath(xpathCbbDeadlineHour())
    Select(element).select_by_visible_text(dlHour)
    
    element=browser.find_element_by_xpath(xpathCbbDeadlineMinute())
    Select(element).select_by_visible_text(dlMinute)
    
    # Auto assign
    element=browser.find_element_by_name(nameChkAutoAssign())
    if assign=='on':
        if element.is_selected():
            pass
        else:
            element.click()
    else:
        if element.is_selected():
            element.click()
        else:
            pass
        
    #description
    frame=browser.switch_to_frame("editor_project_data_ifr")
    element=browser.find_element_by_xpath(".//*[@id='tinymce']")
    element.send_keys(Keys.CONTROL,'a')
    element.send_keys(Keys.DELETE)
    element.send_keys(description)
    
    # submit
    browser.switch_to_default_content()
    element=browser.find_element_by_xpath(xpathBtnSubmitProject())
    element.click()
    
# back to project page
def clickOnLinkProjects(self):
    browser=self.browser
    element=browser.find_element_by_xpath(xpathLinkProjects())
    element.click()
    
# verify project is added
def isProjectAddedSuccessfully(self,project):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[text()='"+project+"']"))
        return True
    except:
        return False
        
# click on a project
def clickOnAProject(self,project):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[text()='"+project+"']")
    element.click()
    
# delete project
def deleteProject(self,project):
    browser=self.browser
    element=browser.find_element_by_xpath("//a[text()='"+project+"']/../../td[6]/img")
    element.click()
    alert=browser.switch_to_alert()
    alert.accept()
    browser.refresh()
    
# verify project is deleted
def isProjectDeletedSuccessfully(self,project):
    browser = self.browser
    try:
        WebDriverWait(browser,20).until(lambda browser : browser.find_element_by_xpath("//a[text()='"+project+"']"))
        return True
    except:
        return False